package com.zpark.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zpark.entity.User;
import com.zpark.mapper.IUserMapper;
import com.zpark.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @PackageName: com.zpark.service.impl
 * @ClassName: UserServiceImpl
 * @Description:
 * @author: RZS
 * @date: 2023/5/11  16:01
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private IUserMapper userMapper;

    @Override
    public Integer addUser(User user) {
        return userMapper.insert(user);
    }

    @Override
    public Integer delUserById(Integer id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer changeUser(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public List<User> findAll() {
        return userMapper.selectAll();
    }

    @Override
    public User findById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<User> findByUnameLike(String uname) {
        return userMapper.selectByUnameLike("%" + uname + "%");
    }

    /**
     * 分页查询
     * @param startPage 起始页码
     * @param num 每页查询的数量
     * @return
     */
    @Override
    public PageInfo<User> selectUserLimit(Integer startPage, Integer num) {
        /**
         *      在执行分页查询语句前执行PageHelper.startPage(page, size)方法
         * 即可实现分页查询。
         * startPage(起始页码, 每页显示数量)
         */
        // 设置分页参数; pageNum:页码, pageSize:每页大小
        PageHelper.startPage(startPage, num);
        // 执行sql查询方法查询所有数据, 会自动分页
        List<User> users = userMapper.selectAll();
        return new PageInfo<User>(users);
    }
}
